package com.ihd.ihardware.view.tzc.me.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.content.Intent;
import android.databinding.ObservableArrayList;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.ihd.ihardware.R;
import com.ihd.ihardware.databinding.ItemMeCollectBinding;
import com.ihd.ihardware.pojo.CollectRes;
import com.ihd.ihardware.view.tzc.discovery.view.BaseWebActivity;
import com.ihd.ihardware.view.tzc.discovery.view.WebActivity;
import com.ihd.ihardware.view.tzc.me.model.CollectRepository;

import java.util.List;

import cn.wowjoy.commonlibrary.adapter.CommonAdapter;
import cn.wowjoy.commonlibrary.utils.GlideRoundTransform;

public class CollectViewModel extends AndroidViewModel {

    private CollectRepository mCollectRepository;

    public CollectViewModel(@NonNull Application application) {
        super(application);
    }


    private ObservableArrayList<CollectRes.DataBean.ListBean> mCollect = new ObservableArrayList<>();
    public CommonAdapter mConcernedAdapter = new CommonAdapter<CollectRes.DataBean.ListBean, ItemMeCollectBinding>(R.layout.item_me_collect, mCollect, new CommonAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View v, int position) {
            if ("1".equals(mCollect.get(position).getType())) {
                Intent i = new Intent(v.getContext(), BaseWebActivity.class);
                i.putExtra("url", mCollect.get(position).getUrl());
                v.getContext().startActivity(i);
            } else {
                Intent i = new Intent(v.getContext(), WebActivity.class);
                i.putExtra("title", mCollect.get(position).getCategoryName() + "秘籍-详情");
                i.putExtra("url", mCollect.get(position).getId());
                i.putExtra("count", mCollect.get(position).getCommentNum());
                i.putExtra("collectState", mCollect.get(position).isCollectState());
                v.getContext().startActivity(i);
            }
        }
    }) {
        @Override
        public void convert(ItemMeCollectBinding mDataBinding, CollectRes.DataBean.ListBean test_collect, int position) {
            Glide.with(mDataBinding.concernedItemLL.getContext())
                    .load(Uri.parse(test_collect.getIconUrl()))
                    .apply(new RequestOptions().transform(new GlideRoundTransform(5)))
                    .into(mDataBinding.head);
        }
    };

    public void getCollectList() {
        if (mCollectRepository == null)
            mCollectRepository = new CollectRepository();
        mCollectRepository.getCollectList();
    }

    public void setCollectList(List<CollectRes.DataBean.ListBean> list) {
        mCollect.clear();
        if (list != null && list.size() > 0)
            mCollect.addAll(list);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        if (null != mCollectRepository) {
            mCollectRepository.onDestroy();
            mCollectRepository = null;
        }
    }
}
